package com.woniuxy.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.RbacManagerDTO;
import com.woniuxy.entity.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import sun.nio.cs.ext.GBK;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @Author: 马宇航
 * @Todo: TODO
 * @DateTime: 22/03/31/0031 上午 10:41
 * @Component: 成都蜗牛学苑
 **/
@Controller
@RequestMapping("/rbacManager")
public class RbacManagerController {
    @RequestMapping("/comeJson")
    public ModelAndView comeJson(@RequestBody RbacManagerDTO rbacManager){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",rbacManager);
        modelAndView.setViewName("boy/heiheihei");
        return modelAndView;
    }
    @RequestMapping("/comeForm")
    public ModelAndView comeForm(String name){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",name);
        modelAndView.setViewName("success");
        return modelAndView;
    }
    @RequestMapping("/comeFormReturnString")
    public String comeFormReturnString(Model model,String name){
        //方案一
        model.addAttribute("msg",name);
        //方案二  二选一即可！
//        modelMap.addAttribute("msg",name);
        return "success";
    }
    @RequestMapping("comeJsonReturnObject")
    @ResponseBody
    public ResponseEntity comeJsonReturnObject(@RequestBody @Validated RbacManagerDTO rbacManager, BindingResult bindingResult){
//       手动对 校验消息的封装！
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            String defaultMessage = fieldError.getDefaultMessage();
            return new ResponseEntity<>(500,defaultMessage,null);
        }else{
            ResponseEntity<RbacManagerDTO> rbacManagerDTOResponseEntity = new ResponseEntity<>(200,"成功",rbacManager);
            return rbacManagerDTOResponseEntity;
        }
    }
    @RequestMapping("testRedirect")
    public String testRedirect(HttpServletResponse response) throws IOException {
        //方式1 使用servlet的模式来重定向
//        response.sendRedirect("/rbacManager/comeFormReturnString?name=123");
//        Spring重定向的写法  redirect:success.jsp 也可以重定向到页面
        String ZZZ = URLEncoder.encode("中文", "UTF-8");
        System.out.println(ZZZ);
//        重定向传中文方法
        return "redirect:/rbacManager/comeFormReturnString?name="+ZZZ;
    }
    @RequestMapping("testForword")
    public String testForword(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //方式1 使用servlet的模式来转发
//        request.getRequestDispatcher("/rbacManager/comeFormReturnString?name=333").forward(request,response);
        return "forward:/WEB-INF/jsp/boy/test.html";
    }
    @RequestMapping("testForword1")
    public String testForword1(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //方式1 使用servlet的模式来转发
//        request.getRequestDispatcher("/rbacManager/comeFormReturnString?name=333").forward(request,response);
        return "forward:/WEB-INF/jsp/boy/heiheihei.jsp";
    }
}
